<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;

/**
 * 来访登记
 *
 */
class SpecificController extends HomeController {

    public function login()
    {
        //获取登录信息
        if (IS_POST) {
            $username=I('post.username');
            $password=I('post.password');
            $uid = D('Agent')->specificlogin($username, $password);
            if(0 < $uid){ //UC登录成功
                $this->success('登录成功！', U('Specific/index'));
            } else { //登录失败
                if($uid==-1){
                    $this->error('用户不存在！', '', true);
                }else{
                    $this->error('用户名或密码不正确！', '', true);
                }
            }

        }else{
            if(is_specific_login()){
                $this->redirect('Specific/index');
            }else{
                $this->display();
            }
        }

    }

    /**
     * 来访登记
     */
    public function index(){
        if(!is_specific_login()){
            $this->error('请先登录',U('login'));
        }
        $major=M('major')->field('id,title')->select();
        $this->assign('data',null);
        $this->assign('major',$major);
        $this->display();
    }



    public function checkfield(){
        $where['ID_number']=I('post.ID_number');
        $where['type']=array('neq',2);
        $data=M('student_info')->where($where)->find();
        if(is_array($data)){
            $this->ajaxReturn(['status'=>1,"info"=>'身份证号已存在']);
        }else{
            $this->ajaxReturn(['status'=>0]);
        }
    }


    //保存信息
    public function saveSudentInfo(){
        $uid=is_specific_login();
        if(!$uid){
            $this->error('请先登录',U('login'));
        }
        if(IS_POST){
            $data=I('post.');
            $where['ID_number']=$data['ID_number'];
            $info=M('student_info')->where($where)->find();
            ($info['type']==2)?$data['type']=5:$data['type']=3;
            if(empty($data['id'])){ //新增数据
                $data['create_time']=time();
                $res = M('student_info')->add($data);
                if(!$res){
                    $this->error ("添加出错");
                }else{
                    $this->success ('添加成功！');
                }
            } else { //更新数据
                $data['update_time']=time();
                $status = M('student_info')->save($data);
                if(false === $status){
                    $this->error ("更新出错");
                }else{
                    $this->success ('更新成功！');
                }
            }
        }
    }



    /* 退出登录 */
    public function logout(){
        if(is_specific_login()){
            session('specific_user_auth', null);
            session('specific_user_auth_sign', null);
            $this->redirect('login');
        } else {
            $this->redirect('login');
        }
    }

}